import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;


class P11321_39643 {


	public static void main(String[] args) {
		
		while (true) {
			
			StringTokenizer st = new StringTokenizer(readLn());
			
			int n = Integer.parseInt(st.nextToken());
			long m = Long.parseLong(st.nextToken());
			
			System.out.println(n + " " + m);
			
			if (n == 0 && m == 0) break;
			
			Long[] array = new Long[n];
			
			for (int k = 0; k < n; k++) {
				array[k] = Long.parseLong(readLn());
			}
			
			Arrays.sort(array, new P11321Comparator(m));
		
			StringBuilder sb = new StringBuilder();
			for (long b : array) sb.append(b + "\n");
			System.out.print(sb.toString());
		}
		
	}

	
	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuilder buffer = new StringBuilder();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString());
	}
	
}

class P11321Comparator implements Comparator<Long> {

	long m;
	
	public P11321Comparator(long m) {
		this.m = m;
	}

	@Override
	public int compare(Long o1, Long o2) {
		long m1 = o1 % m;
		long m2 = o2 % m;
		if (m1 != m2 ) {
			if (m1 < m2) return -1;
			else return +1;
		}
		
		if (Math.abs(o1 % 2) != Math.abs(o2 % 2)) {
			if (o1 % 2 != 0) return -1;
			else return +1;
		} else if (o1 % 2 != 0) {
			if (o1 > o2) return -1;
			else return +1;
		} else { if (o1 < o2) return -1;
			else return +1;
		}
		
	}
	
}